package com.example.animationdemo;

import android.animation.IntEvaluator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private TextView tv;
    private ListView lv;
    private String[] data = {"1", "2", "3", "4", "5", "6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ObjectAnimatorActivity.class);
                startActivity(intent);
            }
        });

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.demo2);
                tv.startAnimation(animation);*/


                /*AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
                alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                alphaAnimation.setDuration(1000);
                tv.startAnimation(alphaAnimation);*/

                /*Rotate3dAnimation rotate3dAnimation = new Rotate3dAnimation(0, 360, 300, 300, 100, true);
                rotate3dAnimation.setDuration(2000);
                tv.startAnimation(rotate3dAnimation);*/

                /*tv.setBackgroundResource(R.drawable.frame_animation);
                final AnimationDrawable animationDrawable = (AnimationDrawable) tv.getBackground();
                animationDrawable.start();
                tv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        animationDrawable.stop();
                    }
                }, 10000);*/
                /*Intent intent = new Intent(MainActivity.this, TestActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);*/

                //ObjectAnimator.ofInt(new ViewWrapper(tv), "width", 500).setDuration(1000).start();

                performAnimate(tv, tv.getWidth(), 500);
            }
        });
        lv = (ListView) findViewById(R.id.list);


        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_layout_item);
        LayoutAnimationController controller = new LayoutAnimationController(animation);
        controller.setDelay(0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        lv.setLayoutAnimation(controller);


        lv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return data.length;
            }

            @Override
            public Object getItem(int position) {
                return data[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = null;
                if (convertView == null) {
                    view = new TextView(MainActivity.this);
                } else {
                    view = convertView;
                }
                ((TextView)view).setText(data[position]);
                return view;
            }
        });
    }

    private static class ViewWrapper {
        private View mTarget;
        public ViewWrapper(View target) {
            this.mTarget = target;
        }

        public int getWidth() {
            return mTarget.getLayoutParams().width;
        }

        public void setWidth(int width) {
            mTarget.getLayoutParams().width = width;
            mTarget.requestLayout();
        }
    }

    private void performAnimate(final View target, final int start, final int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            //持有1个IntEvaluator对象，方便下面估值的时候用
            private IntEvaluator mEvaluator = new IntEvaluator();
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //获得当前动画的进度值，整型，1-100之间
                int currentValue = (int) animation.getAnimatedValue();
                Log.e(TAG, "onAnimationUpdate: currentValue:" + currentValue);

                // 获得当前进度占整个动画过程的比例
                float fraction = animation.getAnimatedFraction();
                target.getLayoutParams().width = mEvaluator.evaluate(fraction, start, end);
                target.requestLayout();
            }
        });
        valueAnimator.setDuration(500).start();
    }
}
